package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.common.response.JsonResult;
import cn.tedu._05weibo.common.response.StatusCode;
import cn.tedu._05weibo.mapper.UserMapper;
import cn.tedu._05weibo.pojo.dto.UserLoginDTO;
import cn.tedu._05weibo.pojo.dto.UserRegDTO;
import cn.tedu._05weibo.pojo.entity.User;
import cn.tedu._05weibo.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.xml.ws.spi.http.HttpContext;
import java.util.Date;
@Api(tags = "01.用户模块")
@Slf4j
@RestController
@RequestMapping("/v1/users/")
public class UserController {
    //自动装配
    @Autowired
    private UserMapper userMapper;

    /**
     * 1.注册功能
     * RequestBody注解：将JavaScript对象转为JSON格式,并封装到DTO中.
     * 是否需要添加此注解的标准：
     * 1.Content-Type: application/x-www-form-urlencoded: 不加!!!
     * 2.Content-Type: application/json: 必须加!!!
     */
    @ApiOperation(value = "注册功能")
    @PostMapping("reg")
    public JsonResult reg(@RequestBody @Validated UserRegDTO userRegDTO) {
        /*
            1.数据合法性校验[此处不做校验];
            2.确认用户名是否被占用[查询接口];
              SELECT id FROM user WHERE username=用户名
              2.1 被占用: return 2;
              2.2 未被占用: 插入数据表[插入接口], return 1;
         */
        log.debug("userRegDTO=" + userRegDTO);
        //soutp + 回车

        //确认用户名是否被占用
        UserVO userVO = userMapper.selectByUsername(userRegDTO.getUsername());
        if (userVO != null) {//被占用
            return new JsonResult(StatusCode.USERNAME_ALREADY_EXISTS);
        }
        //存入数据
        User user = new User();
        BeanUtils.copyProperties(userRegDTO, user);
        user.setCreated(new Date());
        userMapper.insertUser(user);


        return  JsonResult.ok(userVO);//注册成功
    }

    /**
     * 2.登录功能
     */
    @ApiOperation(value = "登录功能")
    @PostMapping("login")
    public JsonResult login(@RequestBody UserLoginDTO userLoginDTO,@ApiIgnore HttpSession session) {
        System.out.println("userLoginDTO = " + userLoginDTO);
        UserVO userVO = userMapper.selectByUsername(userLoginDTO.getUsername());
        if (userVO == null) {
            return new JsonResult(StatusCode.USERNAME_ERROR);
        }//用户名错误
        //比较密码
        if (userLoginDTO.getPassword().equals(userVO.getPassword())) {
            session.setAttribute("user", userVO);
            return new JsonResult(StatusCode.LOGIN_SUCCESS);//登录成功
        }
        return new JsonResult(StatusCode.PASSWORD_ERROR);//密码错误
    }

    /**
     * 3.获取当前用户功能
     */
    @ApiOperation(value = "获取当前用户")
    @GetMapping("currentUser")
    public JsonResult currentUser(@ApiIgnore HttpSession session) {
        UserVO userVO = (UserVO) session.getAttribute("user");
        return  JsonResult.ok(userVO);
    }

    /**
     * 4.退出登录功能
     */
    @ApiOperation(value = "退出登录")
    @GetMapping("logout")
    public void logout(@ApiIgnore HttpSession session) {
        session.removeAttribute("user");
    }

}







